<template>
  <div class="login-page">
    <div id="Login" class="login-container">
      <el-form :model="loginForm" label-width="80px">
        <h1 style="padding-bottom: 30px;">
          医院综合业务管理系统
        </h1>
        <el-form-item label="用户名"  prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" size="large"/>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="margin-bottom: 10px">
          <el-input type="password" v-model="loginForm.userpass" placeholder="请输入密码" size="large"/>
        </el-form-item>
        <el-form-item label="角色" prop="radio1" style="margin-bottom: 0">
          <el-radio-group v-model="radio1">
            <el-radio value="1" size="large">患者</el-radio>
            <el-radio value="2" size="large">医生</el-radio>
            <el-radio value="3" size="large">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="" style="margin-bottom: 0;margin-left: 120px">
          <el-link :underline="false" href="/register" v-show="radio1 == 1" >没有账户？立即注册</el-link>
        </el-form-item>
        <el-form-item style="padding-left: 40px;padding-top: 10px;">
          <el-button type="primary" @click="submitForm" size="large">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import {ElMessage} from "element-plus";
import {loginCheckAdmin, loginCheckDoctor, loginCheckPatient} from "@/api/login.js";

export default {
  name: "login",
  data() {
    return {
      loginForm: {},
      radio1: "1"
    }
  },
  methods: {
    submitForm() {//提交表单登录
      this.login();
    },
    async login() {//登录逻辑判断
      try {
        let response = {};
        if (this.radio1 == 1) {
          response = await await loginCheckPatient(this.loginForm.username, this.loginForm.userpass);
        } else if (this.radio1 == 2) {
          response = await loginCheckDoctor(this.loginForm.username, this.loginForm.userpass);
        } else {
          response = await loginCheckAdmin(this.loginForm.username, this.loginForm.userpass);
        }
        if (response.data.code == 200) {
          ElMessage({
            message: '登陆成功!' + '欢迎您' + this.loginForm.username,
            type: 'success',
          });
          this.$cookies.set('token', response.data.data, '3h');
          if (this.radio1 == 1) {
            this.$cookies.set('type', "pat", '3h');
            this.$router.push("/patientHome")
          } else  if (this.radio1 == 2) {
            this.$cookies.set('type', "doc", '3h');
            this.$router.push("/doctorHome/callNum")
          } else {
            this.$cookies.set('type', "admin", '3h');
            this.$router.push("/adminHome")
          }
        } else {
          ElMessage({
            message: response.data.msg,
            type: 'error',
          });
        }
      } catch (e) {
        ElMessage({
          message: '出现异常！',
          type: 'error',
        });
      }
    }
  }
}
</script>

<style scoped>
.login-page {
  background-image: url("/a.jpg");
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center; /* 水平居中 */
  min-height: 100vh; /* 垂直居中 */
}

.login-container {
  width: 500px;
  height: 410px;
  margin: 0 auto;
  text-align: center;
  background: #f0f2f5; /* 设置登录容器的背景色 */
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
  display: flex;
  flex-direction: column; /* 垂直居中时，容器内部内容垂直排列 */
  justify-content: center; /* 内部元素垂直居中 */
  align-items: center; /* 内部元素水平居中 */
}
</style>
